package com.duanjw.springboot.params.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.duanjw.springboot.params.pojo.dto.UserDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author duanjw
 */
@RestController
@RequestMapping("user")
public class UserController {
//    /**
//     * 获取用户
//     * curl localhost:8080?id=1&name=张
//     * @param userDTO
//     * @return
//     */
//    @GetMapping
//    public UserDTO getUser(UserDTO userDTO) {
//        return userDTO;
//    }
    /**
     * 获取用户
     * curl localhost:8080?id=1&name=张
     * @param request
     * @return
     */
    @GetMapping
    public Map getUser(HttpServletRequest request) {
        Logger log = LoggerFactory.getLogger(DemoController.class);
        Map<String, String[]> parameterMap = request.getParameterMap();
        parameterMap.forEach((key,value) -> {
            log.info("请求参数 key：{}，value：{}", key, value);
        });
        return parameterMap;
    }
    /**
     * 根据 id 获取用户
     * curl localhost:8080?id=1&name=张
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public UserDTO getUserById(Integer id) {
        return new UserDTO(id);
    }
    /**
     * 新增用户
     * curl localhost:8080?id=1&name=张
     * @param userDTO
     * @return
     */
    @PostMapping
    public UserDTO saveUser(UserDTO userDTO) {
        return userDTO;
    }
}
